<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Delete : DataMapper ORM - User Guide</title>

<link rel="shortcut icon" type="image/png" href="../images/favicon.png" />
<link rel="stylesheet" type="text/css" media="all" href="../css/userguide.css" />
<link rel="alternate" type="application/rss+xml" title="Datamapper ORM Updates Feed" href="/rss.xml" />

<meta http-equiv="expires" content="-1" />
<meta http-equiv= 'pragma' content="no-cache" />
<meta name="robots" content="all" />

</head>

<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top">&nbsp;</a><a id="nav_toggle" href="#"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>DataMapper ORM</h1></td>
<td id="breadcrumb_right"><a href="toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->

<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="/">Datamapper ORM Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Delete
</td>
</tr>

</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>Delete</h1>

<p>There are a number of ways to run Delete and its effect will be different depending on whether you pass in a parameter. It's very similar to the <a href="save.html">Save</a> function.</p>
<p class="important"><strong><em>Important:</em></strong> Delete should only be used on existing objects.</p>

<h2>Delete on an Existing Object</h2>
<p>Running Delete on an existing object will delete its corresponding record from the database.</p>
<p class="note"><strong><em>Note:</em></strong> When you delete an object, all its relations to other objects will also be deleted. Free house cleaning! :)</p>
<pre>
<samp>// Get user foo
</samp><var>$u </var><kbd>= new </kbd><var>User</var><kbd>();
</kbd><var>$u</var><kbd>-&gt;</kbd><var><u>where</u></var><kbd>(</kbd><dfn>'username'</dfn><kbd>, </kbd><dfn>'foo'</dfn><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();

</kbd><samp>// Delete user
</samp><var>$u</var><kbd>-&gt;</kbd><var><u>delete</u></var><kbd>();</kbd>
</pre>

<h2 id="Relationship">Delete a Simple Relationship on an Existing Object</h2>

<p>It's easy to delete the relationships your objects have with each other, and there are a few ways of doing it. It's </p>
<p class="important"><strong><em>Important:</em></strong> You can only delete relations from objects that already exist in the Database.</p>

<h3>Delete a Single Relation</h3>

<p>To delete a relation, you pass the object you want to delete the relation to, into your current object.</p>
<pre>
<samp>// Get user foo
</samp><var>$u </var><kbd>= new </kbd><var>User</var><kbd>();
</kbd><var>$u</var><kbd>-&gt;</kbd><var><u>where</u></var><kbd>(</kbd><dfn>'username'</dfn><kbd>, </kbd><dfn>'foo'</dfn><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();

</kbd><samp>// Get country object for Australia
</samp><var>$c </var><kbd>= new </kbd><var>Country</var><kbd>();
</kbd><var>$c</var><kbd>-&gt;</kbd><var><u>where</u></var><kbd>(</kbd><dfn>'name'</dfn><kbd>, </kbd><dfn>'Australia'</dfn><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();

</kbd><samp>// Delete relation between user foo and country Australia
</samp><var>$u</var><kbd>-&gt;</kbd><var><u>delete</u></var><kbd>(</kbd><var>$c</var><kbd>);</kbd>
</pre>

<h3>Delete Multiple Relations</h3>

<p>To delete multiple relations, you pass an object's all property or an array of objects.</p>
<pre>
<samp>// Get user foo
</samp><var>$u </var><kbd>= new </kbd><var>User</var><kbd>();
</kbd><var>$u</var><kbd>-&gt;</kbd><var><u>where</u></var><kbd>(</kbd><dfn>'username'</dfn><kbd>, </kbd><dfn>'foo'</dfn><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();


</kbd><samp>// Get country object for Australia
</samp><var>$c </var><kbd>= new </kbd><var>Country</var><kbd>();
</kbd><var>$c</var><kbd>-&gt;</kbd><var><u>where</u></var><kbd>(</kbd><dfn>'name'</dfn><kbd>, </kbd><dfn>'Australia'</dfn><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();

</kbd><samp>// Get a number of books from the year 2000
</samp><var>$b </var><kbd>= new </kbd><var>Book</var><kbd>();
</kbd><var>$b</var><kbd>-&gt;</kbd><var><u>where</u></var><kbd>(</kbd><dfn>'year'</dfn><kbd>, </kbd><var>2000</var><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();

</kbd><samp>// Get a movie with ID of 5
</samp><var>$m </var><kbd>= new </kbd><var>Movie</var><kbd>();
</kbd><var>$m</var><kbd>-&gt;</kbd><var><u>where</u></var><kbd>(</kbd><dfn>'id'</dfn><kbd>, </kbd><var>5</var><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();

</kbd><samp>// Delete relation between user foo and all the books
</samp><var>$u</var><kbd>-&gt;</kbd><var><u>delete</u></var><kbd>(</kbd><var>$b</var><kbd>-&gt;</kbd><var><i>all</i></var><kbd>);

</kbd><samp>// Or we could pass everything in one go (it's ok to have a mix of single objects and all lists from objects)
</samp><var>$u</var><kbd>-&gt;</kbd><var><u>delete</u></var><kbd>(array(</kbd><var>$c</var><kbd>, </kbd><var>$b</var><kbd>-&gt;</kbd><var><i>all</i></var><kbd>, </kbd><var>$m</var><kbd>));</kbd>
</pre>

<h2 id="Advanced">Delete an Advanced Relationship on an Existing Object</h2>

<p>Just like the advanced saving, you use specialized methods to delete advanced relationships.</p>

<h3>$object->delete_{$relationship_key}( $related )</h3>
<p>Deletes a single <dfn>$related</dfn> as a <var><s>$relationship_key</s></var> from <var>$object</var>.</p>
<ul>
	<li><var><s>{$relationship_key}</s></var>: Replace with the relationship key you want to delete from.</li>
	<li><dfn>$related</dfn>: The object to delete.</li>
</ul>
<pre>
<samp>// Create Post
</samp><var>$post </var><kbd>= new </kbd><var>Post</var><kbd>();
</kbd><samp>// delete $user from the creator
</samp><var>$post</var><kbd>-&gt;</kbd><var><u>delete_</u><s>creator</s></var><kbd>(</kbd><dfn>$user</dfn><kbd>);</kbd>
</pre>

<h3>$object->delete_{$relationship_key}( $array )</h3>
<p>Deletes an <dfn>$array</dfn> of related objects as <var><s>$relationship_key</s></var>s from <var>$object</var>.</p>
<ul>
	<li><var><s>{$relationship_key}</s></var>: Replace with the relationship key you want to delete from.</li>
	<li><dfn>$array</dfn>: The objects to delete.</li>
</ul>
<pre>
<samp>// Create Post
</samp><var>$post </var><kbd>= new </kbd><var>Post</var><kbd>();
</kbd><samp>// Load in related posts.
</samp><var>$relatedposts </var><kbd>= new </kbd><var>Post</var><kbd>();
</kbd><var>$relatedposts</var><kbd>-&gt;</kbd><var><u>where_in</u></var><kbd>(</kbd><var>$related_ids</var><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();
</kbd><samp>// delete related posts
</samp><var>$post</var><kbd>-&gt;</kbd><var><u>delete_</u><i>relatedpost</i></var><kbd>(</kbd><var>$relatedposts</var><kbd>-&gt;</kbd><dfn>all</dfn><kbd>);</kbd>
</pre>


<h3>$object->delete( $related, $relationship_key )</h3>
<p>Delete one or more <dfn>$related</dfn> as a <var><s>$relationship_key</s></var> from <var>$object</var>.</p>
<ul>
	<li><dfn>$related</dfn>: The object or objects to delete.</li>
	<li><var><s>$relationship_key</s></var>: The relationship key you want to delete from.</li>
</ul>
<pre>
<samp>// Create Post
</samp><var>$post </var><kbd>= new </kbd><var>Post</var><kbd>();
</kbd><samp>// Load in related posts.
</samp><var>$relatedposts </var><kbd>= new </kbd><var>Post</var><kbd>();
</kbd><var>$relatedposts</var><kbd>-&gt;</kbd><var><u>where_in</u></var><kbd>(</kbd><var>$related_ids</var><kbd>)-&gt;</kbd><var><u>get</u></var><kbd>();
</kbd><samp>// delete related posts
</samp><var>$post</var><kbd>-&gt;</kbd><var><u>delete</u><kbd>(</kbd><var>$relatedposts</var><kbd>, <var><s>'relatedpost'</s></var><kbd>);</kbd>
</pre>


<h3>Deleting a variety of objects</h3>
<p>Finally, you can use associative arrays to delete a variety of different relationshups</p>
<pre>
<samp>// Create Post
</samp><var>$post </var><kbd>= new </kbd><var>Post</var><kbd>();

</kbd><samp>// delete $user from the creator and editor, and delete related posts.
</samp><var>$post</var><kbd>-&gt;</kbd><var><u>delete</u></var><kbd>(
    array(
        </kbd><var><s>'creator'</s></var> <kbd>=&gt; </kbd><dfn>$user</dfn><kbd>,
        </kbd><var><s>'editor'</s></var> <kbd>=&gt; </kbd><dfn>$user</dfn><kbd>,
        </kbd><var><s>'relatedpost'</s></var> <kbd>=&gt; </kbd><var>$relatedposts</var><kbd>-&gt;</kbd><dfn>all</dfn><var>
    </var><kbd>)
);</kbd>
</pre>

</div>
<!-- END CONTENT -->


<div id="footer">
<p>
<span id="footer_previous">Previous Topic:&nbsp;&nbsp;<a href=""></a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;</span>
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>
<span id="footer_next">&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href=""></a></span>
</p>
<div id="copyrights">
<p><a href="/">Datamapper ORM</a> &nbsp;&middot;&nbsp; Copyright &copy; 2010-2011 &nbsp;&middot;&nbsp; Harro "WanWizard" Verton</p>
<p><a href="license.html">Other License Information</a></p>
</div>
</div>

<script type="text/javascript" src="../js/mootools.js"></script>
<script type="text/javascript" src="../js/menu.js"></script>
<script type="text/javascript">
<!--
	window.addEvent('domready', function() {

		// Create Menu
		var menu = new Menu({
			basepath: '../',
			pagespath: ''
		});

	});
//-->
</script>
</body>
</html>
